package com.ijiuchuang.jc_localization.rx;

import com.ijiuchuang.jc_localization.bean.event.BeaconTagData;
import com.ijiuchuang.jc_localization.tools.utils.HexUtil;

import rx.functions.Func1;

/**
 * Created by Veev on 2016/6/6
 *
 * @link 384506557@qq.com
 */
public class MapOnBytesToBeaconTagData implements Func1<byte[], BeaconTagData> {

    private static volatile MapOnBytesToBeaconTagData sInst = null;

    public static MapOnBytesToBeaconTagData getInstance() {
        MapOnBytesToBeaconTagData inst = sInst;
        if (inst == null) {
            synchronized (MapOnBytesToBeaconTagData.class) {
                inst = sInst;
                if (inst == null) {
                    inst = new MapOnBytesToBeaconTagData();
                    sInst = inst;
                }
            }
        }
        return inst;
    }

    private MapOnBytesToBeaconTagData() {
    }

    @Override
    public BeaconTagData call(byte[] bytes) {

        BeaconTagData beaconTagData = new BeaconTagData();

        //扫描信息
        byte[] beaconMacBytes = new byte[6];
        System.arraycopy(bytes, 0, beaconMacBytes, 0, 6);
        String hexString = HexUtil.encodeHexStr(beaconMacBytes);
        beaconTagData.beaconUuid = hexString;

        byte[] tagMacBytes = new byte[6];
        System.arraycopy(bytes, 6, tagMacBytes, 0, 6);
        beaconTagData.tagUuid = HexUtil.encodeHexStr(tagMacBytes);

        if (bytes[12] < 0) {
            beaconTagData.rssi = 0;
            beaconTagData.rssi = bytes[12];
        }

        beaconTagData.battery = bytes[13];
        beaconTagData.heartRate = bytes[14];
        beaconTagData.alarm = bytes[15];
        beaconTagData.packetFlags = bytes[16];

        return beaconTagData;
    }
}
